#!/bin/bash

# 完整功能测试脚本

echo "🧪 智能对话助手完整功能测试"
echo "========================================"

cd /Users/colinwang/workspace/project/mcp-go/demo2

# 检查Redis
echo "📡 检查Redis连接..."
if ! redis-cli ping > /dev/null 2>&1; then
    echo "❌ Redis未运行，请先启动Redis服务器："
    echo "   redis-server"
    exit 1
fi
echo "✅ Redis连接正常"

# 编译项目
echo "🔨 编译项目..."
go mod tidy
go build -o mcpserver/mcpserver mcpserver/main.go
go build -o server/server server/main.go

if [ $? -ne 0 ]; then
    echo "❌ 编译失败"
    exit 1
fi
echo "✅ 编译成功"

# 测试MCP服务器
echo ""
echo "🔧 测试MCP服务器功能..."
echo "----------------------------------------"

echo "📋 测试1: 获取工具列表"
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | ./mcpserver/mcpserver | head -1
echo ""

echo "📋 测试2: 时间查询"
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_time","arguments":{"location":"北京"}}}' | ./mcpserver/mcpserver | head -1
echo ""

echo "📋 测试3: 数学计算"
echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"calculate","arguments":{"expression":"15+27"}}}' | ./mcpserver/mcpserver | head -1
echo ""

# 启动Web服务器进行测试
echo "🌐 启动Web服务器进行API测试..."
echo "----------------------------------------"

# 使用8082端口避免冲突
SERVER_PORT=8082 ./server/server &
SERVER_PID=$!
echo "服务器PID: $SERVER_PID"

# 等待服务器启动
sleep 3

# 测试API接口
echo "📋 测试4: 检查服务器状态"
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8082/)
if [ "$HTTP_STATUS" = "200" ]; then
    echo "✅ Web服务器运行正常"
else
    echo "❌ Web服务器状态异常: $HTTP_STATUS"
fi

echo ""
echo "📋 测试5: 测试普通聊天API"
RESPONSE=$(curl -s -X POST http://localhost:8082/api/chat \
  -H "Content-Type: application/json" \
  -d '{"sessionId":"test-session","message":"你好"}')

if echo "$RESPONSE" | grep -q "content"; then
    echo "✅ 普通聊天API正常"
else
    echo "❌ 普通聊天API异常"
    echo "响应: $RESPONSE"
fi

echo ""
echo "📋 测试6: 测试时间查询功能"
RESPONSE=$(curl -s -X POST http://localhost:8082/api/chat \
  -H "Content-Type: application/json" \
  -d '{"sessionId":"test-session","message":"现在北京时间是几点？"}')

if echo "$RESPONSE" | grep -q "北京"; then
    echo "✅ 时间查询功能正常"
else
    echo "❌ 时间查询功能异常"
    echo "响应: $RESPONSE"
fi

echo ""
echo "📋 测试7: 测试计算功能"
RESPONSE=$(curl -s -X POST http://localhost:8082/api/chat \
  -H "Content-Type: application/json" \
  -d '{"sessionId":"test-session","message":"计算 10 + 20"}')

if echo "$RESPONSE" | grep -q "30"; then
    echo "✅ 计算功能正常"
else
    echo "❌ 计算功能异常"
    echo "响应: $RESPONSE"
fi

echo ""
echo "📋 测试8: 测试SSE流式API"
curl -s -X POST http://localhost:8082/api/chat/stream \
  -H "Content-Type: application/json" \
  -H "Accept: text/event-stream" \
  -d '{"sessionId":"test-session","message":"你好，请简单介绍一下自己"}' \
  | head -10 | grep -q "event:"

if [ $? -eq 0 ]; then
    echo "✅ SSE流式API正常"
else
    echo "❌ SSE流式API异常"
fi

# 停止服务器
echo ""
echo "🛑 停止测试服务器..."
kill $SERVER_PID 2>/dev/null
wait $SERVER_PID 2>/dev/null

echo ""
echo "✅ 所有测试完成！"
echo ""
echo "🚀 启动应用："
echo "   ./start.sh"
echo ""
echo "🌐 访问地址："
echo "   http://localhost:8080"
echo ""
echo "💡 功能特性："
echo "   ✓ DeepSeek AI对话"
echo "   ✓ MCP工具调用"
echo "   ✓ 时间查询"
echo "   ✓ 数学计算"
echo "   ✓ Server-Sent Events流式响应"
echo "   ✓ WebSocket实时通信"
echo "   ✓ Redis数据存储"
